export const getNewDateArr = (arr) => {
	if (!Array.isArray(arr)) return {};

	// 转换为iOS兼容的日期格式
	const toIosDate = (dateStr) => {
		if (!dateStr) return new Date(NaN);
		// 将 "2025-04-17 15:09:33" 转为 "2025/04/17 15:09:33"
		return new Date(dateStr.replace(/-/g, '/'));
	};

	// 排序
	const sortedArr = [...arr].sort((a, b) => {
		return toIosDate(b.createTime) - toIosDate(a.createTime);
	});

	// 按日期分组
	return sortedArr.reduce((acc, current) => {
		if (!current.createTime) return acc;

		const date = current.createTime.split(' ')[0];
		if (!acc[date]) {
			acc[date] = [];
		}
		acc[date].push(current);
		return acc;
	}, {});
}

// 调用 getNewDateArr 函数并获取最新两个日期对应的数据
export const getLatestTwoData = (arr) => {
	const groupedData = getNewDateArr(arr);
	const dates = Object.keys(groupedData).sort((a, b) => new Date(b) - new Date(a));
	const latestTwoDates = dates.slice(0, 2);
	const latestTwoData = latestTwoDates.reduce((result, date) => {
		result[date] = groupedData[date];
		return result;
	}, {});
	return latestTwoData;
};